var prefix = jsctx + "/invoiceProxyStation";

var invoiceProxyStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageInvoiceProxyStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            stationNameSearch: $('#stationNameSearch').val(),
                            beginTimeSearch: $('#beginTimeSearch').val(),
                            endTimeSearch: $('#endTimeSearch').val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '桩群', width: '150', align: 'left'},
                        {field: 'invoiceProxyTime', title: '添加时间', width: '150', align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '';
                                var b = '';
                                var c = '';
                                if (OperationsManagement_invoiceProxyStation_look) {
                                    a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="开票记录" onclick="invoiceProxyStation.invoiceRecord('
                                        + row.elecStationId
                                        + ')">开票记录</a> ';
                                }
                                if (OperationsManagement_invoiceProxyStation_update) {
                                    b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="取消站点" onclick="invoiceProxyStation.updateInvoiceProxyStationStatus('
                                        + row.elecStationId
                                        + ')">取消站点</a> ';
                                    c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="开票账号" onclick="invoiceProxyStation.showAccountSettingModal(\''
                                        + row.elecStationId + '\',\'' + row.tenantCode + '\',\'' + row.invoiceProxyTenantCode
                                        + '\')">开票账号</a> ';
                                }
                                return a + b + c;
                            }
                        }
                    ]
                });
    };

    function showModel() {
        $('#exampleModal').modal('show')
        $("#elecStationId + div").remove()
        $('#elecStationId').searchableSelect();
        $("#addInvoiceProxyStation").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("elecStationId", $("#elecStationId").val());
            $.ajax({
                url: prefix + "/saveInvoiceProxyStation",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })
    }

    function updateInvoiceProxyStationStatus(elecStationId) {
        layer.confirm('确定要取消站点？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateInvoiceProxyStationStatus",
                type: "post",
                data: {
                    'elecStationId': elecStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("更改成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    //开票账号模态框获取数据
    function showAccountSettingModal(elecStationId, tenantCode, invoiceProxyTenantCode) {
        $('#accountSettingModal').modal('show')
        $.ajax({
            url: prefix + "/listOperatorByStationId",
            type: "GET",
            data: {
                'elecStationId': elecStationId
            },
            success: function (data) {
                if (data.status == 200) {
                    $("#tenantOperatorCode").html("")
                    var str = '<option value="" >分销商账号</option>'
                    if (data.data) {
                        for (var i = 0; i < data.data.length; i++) {
                            if (data.data[i].select == "0") {
                                str += "<option selected value=" + data.data[i].tenantOperatorCode + " >" + data.data[i].loginName + "↲" + data.data[i].contactName + "↲" + data.data[i].operatorName + "</option>";
                            } else {
                                str += "<option value=" + data.data[i].tenantOperatorCode + " >" + data.data[i].loginName + "↲" + data.data[i].contactName + "↲" + data.data[i].operatorName + "</option>";
                            }


                        }
                        $("#tenantOperatorCode").append(str);
                        $("#tenantOperatorCode + div").remove()
                        $('#tenantOperatorCode').searchableSelect();
                        if (invoiceProxyTenantCode != null && tenantCode != invoiceProxyTenantCode) {//分销商
                            $("input[name='tenantType']")[1].checked = true;
                            layui.form.render();
                            //根据状态显示
                            $("#accountSettingModal .searchable-select").removeClass("hide");
                        } else {//本运营商
                            $("input[name='tenantType']")[0].checked = true;
                            layui.form.render();
                            $("#accountSettingModal .searchable-select").addClass("hide");
                        }
                    }
                } else {
                    layer.msg(data.message);
                }
            }
        });

        //开票账号保存
        $("#saveAccountSetting").unbind("click").bind("click", function () {
            var formData = new FormData();
            formData.append("tenantType", $("[name='tenantType']:checked").val());
            formData.append("elecStationId", elecStationId);
            formData.append("tenantOperatorCode", $("#tenantOperatorCode").val());
            $.ajax({
                url: prefix + "/saveAccountSetting",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#accountSettingModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#accountSettingModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#accountSettingModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })
    }


    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    };

    function goBack() {
        location.href = prefix + "/index";
    };

    function invoiceRecord(elecStationId) {
        location.href = prefix + "/invoiceRecord/" + elecStationId;
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        showModel: showModel,
        updateInvoiceProxyStationStatus: updateInvoiceProxyStationStatus,
        showAccountSettingModal: showAccountSettingModal,
        invoiceRecord: invoiceRecord
    };
})();



